package com.itboye.sunsun.message.ui;

import java.util.ArrayList;
import java.util.List;

import com.itboye.sunsunhome.R;
import com.itboye.sunsun.adapter.SystemAnnounceAdapter;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.MessageBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DebugToast;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;
import com.itboye.sunsun.web.ui.WebActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class SystemAnnounceActivity extends BaseActivity {

	ListView listivew;
	BaseAdapter adapter;
	List<MessageBean>dataList=new ArrayList<>();
	String baseUrl="http://sunsun.itboye.com/index.php/Webview/index/systemMessage?";
	String url;
	String psw=(String) SPUtils.get(App.ctx, null, SPContants.PASSWORD, "");
	String uid=(String) SPUtils.get(App.ctx, null, SPContants.USER_ID, "");
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_sysytem_announce);
		adapter=new SystemAnnounceAdapter(dataList   );
		listivew.setAdapter(adapter);
		 getData() ;
		 
		 listivew.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(getApplicationContext(), WebActivity.class);
				intent.putExtra("url",  baseUrl+"id="+dataList.get(position).getMsgId()+"&psw="+psw+"&uid="+uid);
				intent.putExtra("title", "我的消息");				
				startActivity(intent); 
			}
		});
		 
		 findViewById(R.id.back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
	
	private void getData() {
		MyJsonRequest<List<MessageBean>>request=new MyJsonRequest.Builder<List<MessageBean>>()
				.typeKey("BY_Message_query")
				.apiVer("100")
				.param("uid",(String)SPUtils.get(getApplicationContext(), null, SPContants.USER_ID	, ""))
				.param("msg_type","1")
				.requestListener(new XRequestListener<List<MessageBean>>() {
					
					@Override
					public void onResponse(List<MessageBean> arg0) {
						 
						if (arg0!=null) { 
							dataList.addAll(arg0);
							adapter.notifyDataSetChanged();
						}
					}
				})
				.errorListener(new XErrorListener() {
					
					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						 
					}
				})
				.build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
}
